from datetime import datetime
from datetime import timedelta
import os
import json
import sys
import re

path_tmpl = "/export/iosg/coin-mirror-1/buckets/feed.raw.coin/{machine}/{date}/{mea}"
# path_tmpl = "/remote/iosg/coin-mirror-1/buckets/feed.raw.coin/{machine}/{date}/{mea}"
priority_sync_config = [
  {"feed-05.ap-northeast-1.aws": ['spot_binance_v1', 'futures_binance_v1', 'spot_okex_v5', 'futures_okex_v5-swap', 'spot_bybit_v3', 'futures_bybit_v3-linear']},
  {"feed-05.cn-hongkong.aliyun":['spot_coingecko_v3']}
]


def get_subscribed_groups(machine, mea):
  path = f"./data/coin2/feed/ops/feed-writer/{machine}.json"
  with open(path, "r") as fp:
    machine_config = json.load(fp)
    mea_split = mea.split('_')
    mea = '.'.join([mea_split[0].title(),mea_split[1].title(),mea_split[2]])
    return machine_config["feed"]["exchanges"][mea]["products"]["groups"]


date_base = datetime.utcnow() - timedelta(minutes=40)
date = date_base.strftime("%Y%m%d")
hour = date_base.strftime("%H")
minute = date_base.strftime("%M")
minute = "30" if int(minute) > 30 else "00"

sync_compelete = True
incomplete = []
for machine_config in priority_sync_config:
  for machine,meas in machine_config.items():
    for mea in meas:
      group_number = len(get_subscribed_groups(machine,mea))
      path = str.format(path_tmpl, machine=machine, date=date, mea=mea)
      if not os.path.exists(path):
        sync_compelete = False
        incomplete.append((machine,mea,0,group_number))
      for root, dirs, files in os.walk(path, topdown=False):
        suffix = date + "-" + hour + minute + r"\d{2}Z.gz"
        traget_file_list = [file for file in files if re.search(suffix,file)]
        file_number = len(traget_file_list)
        if file_number != group_number*2 and file_number != group_number:
          sync_compelete = False
          incomplete.append((machine,mea,file_number,group_number))
      
if sync_compelete :
  sys.exit(0)
print(f"{incomplete} sync incomplete!")
sys.exit(1)